/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import dbconnector.DBDriver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

/**
 *
 * @author FRIENDS
 */
public class exam
{
    static PreparedStatement p,pstmt;
    static ResultSet rs;
    
    public static AO.Exam getExambean(String examid)
    {
        
          try{
         p= DBDriver.getPrepare("select * from exam where examid=?");
         p.setString(1, examid);
         rs=p.executeQuery();
         AO.Exam exam=new AO.Exam();
         if(rs.next())
         { 
            exam.setExamid(rs.getString(1));
            exam.setExamdate(rs.getString(2));
            exam.setExamduration(rs.getLong(3));
            exam.setCourseid(rs.getString(4)); 
            exam.setUserid(rs.getString(5));
         }
         return exam;
}catch(Exception e){e.printStackTrace();return null;}
    
    }
    

public static int remainingDays(String examid)
{
    try
    {
     p=DBDriver.getPrepare("select DATEDIFF(examdate,now()) from exam where examid=?");
     p.setString(1, examid);
     rs=p.executeQuery();
     if(rs.next())
         return rs.getInt(1);
     return 0;
    }
    catch(Exception e){e.printStackTrace();return 0;}
    
}
  
    
    
    public static String getNewExamId()
{
    try
    {
        p=DBDriver.getPrepare("select max(examid) from exam");
        rs=p.executeQuery();
        if(rs.next())
            if(rs.getString(1)!=null)
            {   
              int i=Integer.parseInt(rs.getString(1).substring(4));
               return "exam"+(++i);
            }
         }
        
    catch(Exception e){return "exam1"; }
    return "exam1";
   
}
    public static int getNoofExams(String courseid)
    {
      try
      {
        p=DBDriver.getPrepare("select 1 from exam where courseid=?");
        p.setString(1, courseid);
        rs=p.executeQuery();
        int i=0;
        while(rs.next())
            i++;
        return i;
      }
      catch(Exception e){return -1; }
   }

    public static void main(String[] a)
    {
     Date starttime=new Date();
     Date enddate=(Date)starttime.clone();
    }
   }